/**
Zeke Elder - zbe@andrew.cmu.edu
Amal Chemmany - achemman@andrew.cmu.edu
Nishant Kumar - nkumar1@andrew.cmu.edu
15-637
4/2/2012
 **/

package edu.cmu.fithub.actions;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import edu.cmu.fithub.controller.Action;
import edu.cmu.fithub.db.DBHelper;
import edu.cmu.fithub.db.bean.Exercise;
import edu.cmu.fithub.db.bean.GroupMembershipNotification;
import edu.cmu.fithub.db.bean.Schedule;
import edu.cmu.fithub.db.bean.User;
import edu.cmu.fithub.db.bean.Workout;
import edu.cmu.fithub.db.bean.WorkoutDisplay;
import edu.cmu.fithub.db.bean.WorkoutHistory;

public class DisplayNotificationAction extends Action {

	public DisplayNotificationAction() {
	}

	public String getName() {
		return "showNotifications.do";
	}

	public String perform(HttpServletRequest request) {

		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);

		User user = (User) request.getSession().getAttribute("user");
		String groupId = request.getParameter("groupId");
		Session dbSession = DBHelper.getSessionFactory().openSession();
		List<GroupMembershipNotification> inviteList = dbSession
				.createCriteria(GroupMembershipNotification.class)
				.add(Restrictions.eq("userId", user.getUserId())).list();
		GroupMembershipNotification[] invites = new GroupMembershipNotification[inviteList
				.size()];
		invites = inviteList.toArray(invites);
		request.setAttribute("invites", invites);

		// retrieve pending workouts
		// 1. Find all workouts for the user
		// 2. Get all workouts completed for the last 7 days from workout
		// history table
		// 3. remove workouts from workoutlist which were completed
		// 4. set pending workouts in request
		List<Workout> workoutList = dbSession.createCriteria(Workout.class)
				.add(Restrictions.eq("userId", user.getUserId())).list();
		Date currentDate = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(currentDate);
		gc.add(Calendar.DAY_OF_WEEK, -6);
		Date lastWeekDate = gc.getTime();
		List<WorkoutHistory> workoutHistoryList = dbSession
				.createCriteria(WorkoutHistory.class)
				.add(Restrictions.eq("userId", user.getUserId()))
				.add(Restrictions.le("dateCompleted", currentDate))
				.add(Restrictions.ge("dateCompleted", lastWeekDate)).list();
		
		
		List<WorkoutDisplay> workoutDisplayList = new ArrayList<WorkoutDisplay>();

		// get all schedule entries for the user
		List<Schedule> sched_list = dbSession.createCriteria(Schedule.class)
				.add(Restrictions.eq("userId", user.getUserId()))
				.addOrder(Order.asc("day_of_week")).list();
		Schedule[] schedList = new Schedule[sched_list.size()];
		schedList = (Schedule[]) sched_list.toArray(schedList);

		// for each schedule entry, get all exercises in the workout
		for (Iterator iterator = sched_list.iterator(); iterator.hasNext();) {
			Schedule schedule = (Schedule) iterator.next();
			WorkoutDisplay tmp_wd = new WorkoutDisplay();
			List<Workout> workout_list = dbSession
					.createCriteria(Workout.class)
					.add(Restrictions.eq("userId", user.getUserId()))
					.add(Restrictions.eq("workoutId", schedule.getWorkoutId()))
					.list();

			tmp_wd.setWorkoutId(workout_list.get(0).getWorkoutId());
			tmp_wd.setId(schedule.getId());
			tmp_wd.setDate(schedule.getDay() + " " + schedule.getHour() + ":"
					+ schedule.getMinute());

			Exercise[] ex = new Exercise[10];
			int[] reps = new int[10];
			int i = 0;
			for (Iterator iterator2 = workout_list.iterator(); iterator2
					.hasNext();) {
				Workout workout = (Workout) iterator2.next();
				ex[i] = workout.getExercise();
				reps[i] = workout.getReps();
				i++;

			}
			tmp_wd.setExercise(ex);
			tmp_wd.setReps(reps);
			workoutDisplayList.add(tmp_wd);
		}
		
		
		List<Integer> workoutIdHistoryList = new ArrayList<Integer>();
		for (int i = 0; i < workoutHistoryList.size(); i++) {
			workoutIdHistoryList.add(workoutHistoryList.get(0).getWorkoutId());
		}

		for (int i = 0; i < workoutDisplayList.size(); i++) {
			if (workoutIdHistoryList
					.contains(workoutDisplayList.get(i).getWorkoutId())) {
				workoutDisplayList.remove(i);
			}
		}

		request.setAttribute("pendingWorkoutList", workoutList);

		return "notifications.jsp";
	}
}
